import {asyncRoutes } from '../../router/index'
export function filterAsyncRoutes(menus) {
    let tempArr = []
    let filterArr = function (arr) {
        arr.children.forEach(item => {
            let a = asyncRoutes[0].children.find(val => val.path === item.url)
            if (a) {
                tempArr.push(a)
            }
            if(item.children) {
                filterArr(item)
            }
        })
    }
    filterArr(menus)
    asyncRoutes[0].children = tempArr
    return asyncRoutes
}




const state = {
    routes: [],
    addRoutes: []
}
const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
    }
}
const actions = {
    generateRoutes({commit}, menu) {
        return new Promise(resolve => {
            let accessedRoutes = filterAsyncRoutes(menu)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })

    }
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}
